package com.shuda.system.controller;

import com.shuda.common.dto.PageDTO;
import com.shuda.common.dto.PageResult;
import com.shuda.common.result.Result;
import com.shuda.system.dto.RoleDTO;
import com.shuda.system.dto.RoleQueryDTO;
import com.shuda.system.service.RoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 角色管理控制器
 */
@RestController
@RequestMapping("/api/system/role")
@Tag(name = "角色管理", description = "角色管理相关接口")
@RequiredArgsConstructor
@Validated
public class RoleController {

    private final RoleService roleService;

    /**
     * 获取角色分页列表
     */
    @PostMapping("/page")
    @Operation(summary = "获取角色分页列表")
    public Result<PageResult<RoleDTO>> getRolePage(@Valid @RequestBody PageDTO pageDTO, RoleQueryDTO queryDTO) {
        PageResult<RoleDTO> result = roleService.getRolePage(pageDTO, queryDTO);
        return Result.success(result);
    }

    /**
     * 根据ID获取角色详情
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取角色详情")
    public Result<RoleDTO> getRoleById(@PathVariable Long id) {
        RoleDTO roleDTO = roleService.getRoleById(id);
        return Result.success(roleDTO);
    }

    /**
     * 创建角色
     */
    @PostMapping
    @Operation(summary = "创建角色")
    public Result<Long> createRole(@Valid @RequestBody RoleDTO roleDTO) {
        Long id = roleService.createRole(roleDTO);
        return Result.success(id);
    }

    /**
     * 更新角色
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新角色")
    public Result<String> updateRole(@PathVariable Long id, @Valid @RequestBody RoleDTO roleDTO) {
        roleService.updateRole(id, roleDTO);
        return Result.success("角色更新成功");
    }

    /**
     * 删除角色
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除角色")
    public Result<String> deleteRole(@PathVariable Long id) {
        roleService.deleteRole(id);
        return Result.success("角色删除成功");
    }

    /**
     * 批量删除角色
     */
    @DeleteMapping("/batch")
    @Operation(summary = "批量删除角色")
    public Result<String> batchDeleteRole(@RequestBody List<Long> ids) {
        roleService.batchDeleteRole(ids);
        return Result.success("角色批量删除成功");
    }

    /**
     * 更新角色状态
     */
    @PutMapping("/{id}/status")
    @Operation(summary = "更新角色状态")
    public Result<String> updateRoleStatus(@PathVariable Long id, @RequestParam Integer status) {
        roleService.updateRoleStatus(id, status);
        return Result.success("角色状态更新成功");
    }

    /**
     * 获取所有启用的角色列表
     */
    @GetMapping("/all")
    @Operation(summary = "获取所有启用的角色列表")
    public Result<List<RoleDTO>> getAllEnabledRoles() {
        List<RoleDTO> roles = roleService.getAllEnabledRoles();
        return Result.success(roles);
    }

    /**
     * 获取用户角色列表
     */
    @GetMapping("/user/{userId}")
    @Operation(summary = "获取用户角色列表")
    public Result<List<RoleDTO>> getUserRoles(@PathVariable Long userId) {
        List<RoleDTO> roles = roleService.getUserRoles(userId);
        return Result.success(roles);
    }

    /**
     * 分配用户角色
     */
    @PutMapping("/user/{userId}/roles")
    @Operation(summary = "分配用户角色")
    public Result<String> assignUserRoles(@PathVariable Long userId, @RequestBody List<Long> roleIds) {
        roleService.assignUserRoles(userId, roleIds);
        return Result.success("用户角色分配成功");
    }

    /**
     * 获取角色菜单列表
     */
    @GetMapping("/{roleId}/menus")
    @Operation(summary = "获取角色菜单列表")
    public Result<List<Long>> getRoleMenuIds(@PathVariable Long roleId) {
        List<Long> menuIds = roleService.getRoleMenuIds(roleId);
        return Result.success(menuIds);
    }

    /**
     * 分配角色菜单
     */
    @PutMapping("/{roleId}/menus")
    @Operation(summary = "分配角色菜单")
    public Result<String> assignRoleMenus(@PathVariable Long roleId, @RequestBody List<Long> menuIds) {
        roleService.assignRoleMenus(roleId, menuIds);
        return Result.success("角色菜单分配成功");
    }
}